package com.offcn.scwproject.controller;

import com.alibaba.fastjson.JSON;
import com.offcn.dycommon.enums.ProjectStatusEnume;
import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwproject.contants.ProjectContants;
import com.offcn.scwproject.pojo.TReturn;
import com.offcn.scwproject.service.ProjectCreateService;
import com.offcn.scwproject.vo.req.ProjectBaseInfoVo;
import com.offcn.scwproject.vo.req.ProjectRedisStorageVo;
import com.offcn.scwproject.vo.req.ProjectReturnVo;
import com.offcn.vo.BaseVo;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping
@Api(tags = "项目的创建-初始化")
@Slf4j
public class ProjectCreateController {
    @Autowired
    private ProjectCreateService projectCreateService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @ApiOperation("项目初始化的第一步")
    @PostMapping("/init")
    public AppResponse init(BaseVo vo){
        String accessToken = vo.getAccessToken();


        String memberId = redisTemplate.opsForValue().get(accessToken);

        if(StringUtils.isEmpty(memberId)){
            return AppResponse.fail("请登录");
        }

        String projectToken = projectCreateService.initCreateProject(Integer.parseInt(memberId));
        return AppResponse.ok(projectToken);
    }
    @ApiOperation("第二步——保存页面的基本信息")
    @PostMapping("/savebaseInfo")
    public AppResponse saveBaseInfo(ProjectBaseInfoVo vo){
        //从redis获取项目
        String redisProject = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + vo.getProjectToken());
        //转换成对象类型
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);
        //把vo的东西--->存入缓存对象
        BeanUtils.copyProperties(vo,projectRedisStorageVo);
        //转换成json字符串
        String s = JSON.toJSONString(projectRedisStorageVo);
        redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+vo.getProjectToken(),s);
        return AppResponse.ok("ok");
    }
    @ApiOperation("第三步——保存回报")
    @PostMapping("/saveretrun")
    public AppResponse saveReturnInfo(@RequestBody List<ProjectReturnVo> pro){
        //拿项目令牌
        String projectToken = pro.get(0).getProjectToken();
        //从redis取项目
        String s = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + projectToken);
        //字符串转换为对象类型
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(s, ProjectRedisStorageVo.class);
        //前端的回报保存到集合里
        List list=new ArrayList();
        for (ProjectReturnVo projectReturnVo : pro) {
            TReturn tReturn = new TReturn();
            BeanUtils.copyProperties(projectReturnVo,tReturn);
            list.add(tReturn);
        }
        //把集合存入临时对象
        projectRedisStorageVo.setProjectReturns(list);
        String s2 = JSON.toJSONString(projectRedisStorageVo);
        redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+projectToken,s2);
        return AppResponse.ok("ok");


    }

    @ApiOperation("项目发起第4步-项目保存项目回报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken",value = "用户令牌",required = true),
            @ApiImplicitParam(name = "projectToken",value="项目标识",required = true),
            @ApiImplicitParam(name="ops",value="用户操作类型 0-保存草稿 1-提交审核",required = true)})
    @PostMapping("/submit")
    public AppResponse<Object> submit(String accessToken,String projectToken,String ops){

        //1、前置校验 检查accessToken
        String memberId = redisTemplate.opsForValue().get(accessToken);
        if(StringUtils.isEmpty(memberId)){
            return AppResponse.fail("请登录");
        }
        //2、根据项目token，获取项目存储在redis的信息
        String redisProject = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + projectToken);
        if(StringUtils.isEmpty(redisProject)){
            return AppResponse.fail("没有此项目");
        }
        //把json格式的项目信息还原为项目对象
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);
        //3、判断用户操作类型不为空，进行处理

        //判断操作类型是1，提交审核
        if(ops.equals("1")){


            //保存项目信息
            projectCreateService.saveProjectInfo(ProjectStatusEnume.SUBMIT_AUTH,projectRedisStorageVo);

            return AppResponse.ok("提交审核成功");
        }else if(ops.equals("0")){
            //0草稿--即将开始
            projectCreateService.saveProjectInfo(ProjectStatusEnume.DRAFT,projectRedisStorageVo);
            return AppResponse.ok("已存入草稿");
        }else{
            return AppResponse.fail("不允许的操作");
        }

        }
}
